
enum ANI_TYPE {
    NULL,
    /** 轻微左右震动 */
    SLIGHT_WAGGLE,
    /** 呼吸缩放 */
    SCALE,
}

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu("ipha组件/简单icon动画01")
export default class Ipha_SlimpleAniIcon extends cc.Component {

    @property({
        type: cc.Enum(ANI_TYPE),
        displayName: "动画类型",
        tooltip: `空
        轻微左右晃动
        呼吸缩放`,
    })
    private aniType: ANI_TYPE = ANI_TYPE.NULL;

    @property({ displayName: "自动开始" })
    private isAutoStart: boolean = true;

    @property({
        displayName: "是否循环播放",
        visible() {
            return this.aniType == ANI_TYPE.SLIGHT_WAGGLE
                || this.aniType == ANI_TYPE.SCALE;
        }
    })
    private isLoop: boolean = true;

    @property({
        displayName: "震动幅度",
        visible() {
            return this.aniType == ANI_TYPE.SLIGHT_WAGGLE;
        }
    })
    private angleValue: Number = 5;

    @property({
        displayName: "震动速度",
        visible() {
            return this.aniType == ANI_TYPE.SLIGHT_WAGGLE;
        }
    })
    private slightWaggleTime: number = 0.1;

    @property({
        displayName: "呼吸缩放 延迟",
        visible() {
            return this.aniType == ANI_TYPE.SCALE;
        }
    })
    private _scaleDelay: number = 1;

    @property({
        displayName: "呼吸缩放 速度",
        visible() {
            return this.aniType == ANI_TYPE.SCALE;
        }
    })
    private _scaleSpeed: number = 0.3;

    @property({
        displayName: "横轴缩放MAX",
        visible() {
            return this.aniType == ANI_TYPE.SCALE;
        }
    })
    private _xScaleMax: number = 0.1;

    @property({
        displayName: "横轴缩放MIN",
        visible() {
            return this.aniType == ANI_TYPE.SCALE;
        }
    })
    private _xScaleMin: number = 0.1;

    @property({
        displayName: "竖轴缩放MAX",
        visible() {
            return this.aniType == ANI_TYPE.SCALE;
        }
    })
    private _yScaleMax: number = 0.1;

    @property({
        displayName: "竖轴缩放MIN",
        visible() {
            return this.aniType == ANI_TYPE.SCALE;
        }
    })
    private _yScaleMin: number = 0.1;

    @property({
        displayName: "循环次数",
        visible() {
            return !this.isLoop;
        }
    })
    private repeatCnt: number = 0;

    private tag: number = 1001;

    start() {
        if (this.isAutoStart) {
            this.playAni();
        }
    }

    playAni() {
        this.aniType != ANI_TYPE.NULL && this.aniList()[this.aniType]();
    }

    aniList() {
        let aL: Function[] = [
            null,
            this.slightWaggle.bind(this),
            this.scale.bind(this),
        ];
        return aL;
    }

    protected slightWaggle() {
        if (this.isLoop) {
            cc.tween(this.node).tag(this.tag).sequence(
                cc.tween().to(this.slightWaggleTime, { angle: this.angleValue }).to(this.slightWaggleTime, { angle: -this.angleValue }),
                cc.tween().to(this.slightWaggleTime, { angle: this.angleValue }).to(this.slightWaggleTime, { angle: -this.angleValue }),
            ).repeatForever().start();
        } else {
            this.repeatCnt = this.repeatCnt ? this.repeatCnt : 1;
            cc.tween(this.node).tag(this.tag).sequence(
                cc.tween().to(this.slightWaggleTime, { angle: this.angleValue }).to(this.slightWaggleTime, { angle: -this.angleValue }),
                cc.tween().to(this.slightWaggleTime, { angle: this.angleValue }).to(this.slightWaggleTime, { angle: -this.angleValue }),
            ).repeat(this.repeatCnt).start();
        }
    }

    protected scale() {
        let nodeScaleX = this.node.scaleX;
        let nodeScaleY = this.node.scaleY;
        if (this.isLoop) {
            cc.tween(this.node).sequence(
                cc.tween().to(this._scaleSpeed, { scaleX: nodeScaleX + this._xScaleMax, scaleY: nodeScaleY + this._yScaleMax }),
                cc.tween().to(this._scaleSpeed, { scaleX: nodeScaleX - this._xScaleMin, scaleY: nodeScaleY - this._yScaleMin }).delay(this._scaleDelay),
            ).repeatForever().start();
        } else {
            cc.tween(this.node).sequence(
                cc.tween().to(this._scaleSpeed, { scaleX: nodeScaleX + this._xScaleMax, scaleY: nodeScaleY + this._yScaleMax }),
                cc.tween().to(this._scaleSpeed, { scaleX: nodeScaleX - this._xScaleMin, scaleY: nodeScaleY - this._yScaleMin }),
            ).repeat(this.repeatCnt).start();
        }
    }

    stopAni() {
        this.node.stopActionByTag(this.tag);
    }

}
